/**
 * 
 */
package com.oron3.bouncer.objects;

import com.oron3.bouncer.genericgl.GLWorld;
import com.oron3.bouncer.objects.shapes.Point;
import com.oron3.bouncer.objects.shapes.Vector;


/**
 * @author Ofek
 *
 */
public class GLBall extends GLSphere {
	public float mu = 0.6f;
	private static final float g = 9.81f;
	private static final float L = 50;
	private static final float RADIUS = 3;
	private static final float EPSILON = 0.1f;
	private final float b = 0.4f;
	public Vector v = new Vector();

	public GLBall(final float x, final float vx, final float y, final float vy, final float z, final float vz, final float mu) {
		super(x,y,z,RADIUS, 0.05f);
		v.set(vx,vy,vz);
		this.mu = mu;
	}

	GLBall(final float x, final float vx, final float y, final float vy, final float z, final float vz) {
		super(x,y,z,RADIUS, 0.05f);
		v.set(vx,vy,vz);
	}

	public GLBall(final float x, final float y, final float z) {
		super(x,y,z,RADIUS, 0.05f);
	}

	/**
	 * @param vx
	 * @param vy
	 * @param vz
	 * @param mu
	 * 
	 */
	public GLBall(final float vx, final float vy, final float vz, final float mu) {
		super(0,0,0,RADIUS, 0.05f);
		v.set(vx,vy,vz);
		this.mu = mu;
	}

	/**
	 * @param f
	 * 
	 */
	public GLBall(final float mu) {
		super(0,0,0,RADIUS, 0.05f);
		this.mu = mu;
	}
	/**
	 * @param point
	 * @param f
	 */
	public GLBall(final Point p, final float mu) {
		super(p.x,p.y,p.z,RADIUS, 0.05f);
		this.mu = mu;
	}

	@Override
	public void step(final float dt,final GLWorld glWorld) {
		center.x += v.x * dt;
		center.y += v.y * dt;
		center.z += v.z * dt;
		for ( final GLShape shape : glWorld ) {
			shape.collision(this,v);
		}
		v.x -= v.x * b * dt;
		v.y -= v.y * b * dt;
		v.z -= (g*dt+v.z*b*dt);
		v.init();

	}
	public void bounce(final Vector n) {
		final Point res = v.reflect(n);
		setVelocity(res.x * mu,res.y * mu, res.z * mu);
	}



	/**
	 * 
	 */
	public void setVelocity(final float vx, final float vy, final float vz) {
		v.x = Math.abs(vx) > EPSILON ? vx : 0;
		v.y = Math.abs(vy)> EPSILON ? vy :0 ;
		v.z = Math.abs(vz)> EPSILON ? vz:0;
		v.init();
	}

	public void setPosition(final Point p) {
		center.x = p.x;
		center.y = p.y;
		center.z = p.z;

	}

	public void move(final float dx, final float dy, final float dz) {
		center.x += dx;
		center.y += dy;
		center.z += dz;
		System.out.println(String.format("moving by %.2f,%.2f,%.2f", dx,dy,dz));
	}
	public void move(final Vector d) {
		center.x += d.x;
		center.y += d.y;
		center.z += d.z;
		System.out.println(String.format("moving by %.2f,%.2f,%.2f", d.x,d.y,d.z));
	}



	/**
	 * @param i
	 * @param j
	 * @param k
	 */
	public void setPosition(final float x, final float y, final float z) {
		center.x = x;
		center.y = y;
		center.z = z;
	}

	@Override
	public String toString() {
		return String.format("ball : P=(%.2f,%.2f,%.2f),V=(%.2f,%.2f,%.2f)",center.x,center.y,center.z,v.x,v.y,v.z);
	}
}
